/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package serverCommunication.controller;

import java.io.BufferedReader;
import java.io.InputStreamReader;
import java.io.PrintWriter;
import java.net.Socket;
import java.util.ArrayList;

import serverCommunication.StringParser.StringToTrafficPoint;
import serverCommunication.StringParser.StringToUser;
import serverCommunication.model.TrafficPoint;
import serverCommunication.model.User;

/**
 *
 * @author RodriguesMoises
 */
public class WebClientInterface {
    private static String host = "192.168.0.174";
    private static int port = 9090;
    
    public static void createUser(User user)
    {
        sendMessage("createUser <Begin>" + user + "<End>");
    }
    
    public static User readUser(String login)
    {
        User user = null;
        user = StringToUser.parse(sendMessage("readUserString <Begin>" + login + "<End>"));
        return user;
    }
    
    public static User readUser(long userId)
    {
        User user = null;
        user = StringToUser.parse(sendMessage("readUserLong <Begin>" + userId + "<End>"));
        return user;
    }
    
    public static void updateUser(User user)
    {
        sendMessage("updateUser <Begin>" + user + "<End>");
    }
    
    public static void deleteUser(long userId)
    {
        sendMessage("deleteUser <Begin>" + userId + "<End>");
    }
    
    public static void createTrafficPoint(TrafficPoint trafficPoint)
    {
        sendMessage("createTrafficPoint <Begin>" + trafficPoint + "<End>");
    }
    
    public static TrafficPoint readTrafficPoint(long pointId)
    {
        TrafficPoint trafficPoint = null;
        trafficPoint = StringToTrafficPoint.parse(sendMessage("readTrafficPoint <Begin>" + pointId + "<End>"));
        return  trafficPoint;
    }
    
    public static void deleteTrafficPoint(long pointId)
    {
        sendMessage("deleteTrafficPoint <Begin>" + pointId + "<End>");
    }
    
    public static ArrayList<TrafficPoint> getAllTrafficPoints()
    {
        ArrayList<TrafficPoint> trafficPoints = StringToTrafficPoint.getTrafficPointsFromString(
                sendMessage("getAllTrafficPoints <Begin><End>"));
        return trafficPoints;
    }
    
    private static String sendMessage(String string)
    {
        String stringRet = "";
        try{
            Socket socket = new Socket(host, port);
            PrintWriter printWriter = new PrintWriter(socket.getOutputStream(), true);
            System.out.println("Sending:\n " + string);
            printWriter.println(string);
            BufferedReader bufferedReader = new BufferedReader(new InputStreamReader(socket.getInputStream()));
            String str = "";
            while((str = bufferedReader.readLine()) != null)
            {
                stringRet += str;
            }
            printWriter.close();
            bufferedReader.close();
            socket.close();
        }catch(Exception e)
        {
            System.err.println(e.getMessage());
        }
        return stringRet;
    }
}
